//
//  ScrollerViewController.m
//  封装网络请求
//
//  Created by 赵凯强 on 2016/11/14.
//  Copyright © 2016年 赵凯强. All rights reserved.
//

#import "ScrollerViewController.h"
#import "ViewController.h"
@interface ScrollerViewController ()<UIScrollViewDelegate>
{
    //登录按钮
    UIButton *loginBut;
}
// 滚动视图
@property (nonatomic ,strong) UIScrollView *scrollerView;
// 图片数组
@property (nonatomic ,strong) NSArray *pictureArr;
// 页面控制器
@property (nonatomic ,strong) UIPageControl *pageControl;

@end

@implementation ScrollerViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    

    [self.view addSubview:self.scrollerView];
    [self.view addSubview:self.pageControl];

}

-(UIScrollView *)scrollerView
{
    if (_scrollerView == nil) {
        _scrollerView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
        for (int a = 0; a < self.pictureArr.count; a++) {
            
            UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(self.view.frame.size.width *a, 0, self.view.frame.size.width, self.view.frame.size.height)];
            imageView.image = [UIImage imageNamed:self.pictureArr[a]];
            [self.scrollerView addSubview:imageView];

        }
        _scrollerView.bounces = NO;
        _scrollerView.pagingEnabled = YES;
        _scrollerView.delegate = self;
    }
    
    _scrollerView.contentSize = CGSizeMake(self.view.frame.size.width *self.pictureArr.count, 0);
    
    return _scrollerView;
}

-(UIPageControl *)pageControl
{
    if (_pageControl == nil) {
        _pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(self.view.frame.size.width/2-50, 670, 100, 50)];
        _pageControl.numberOfPages = self.pictureArr.count;
        _pageControl.pageIndicatorTintColor = [UIColor grayColor];
        _pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
        [_pageControl addTarget:self action:@selector(clickPage) forControlEvents:UIControlEventValueChanged];
    }
    return _pageControl;
}

-(NSArray *)pictureArr
{
    if (_pictureArr == nil) {
        
        
        _pictureArr = @[@"2.png",@"3.png",@"4.png",@"5.png"];
    }
    return _pictureArr;
}

-(void)clickPage
{
    self.scrollerView.contentOffset = CGPointMake(self.pageControl.currentPage *self.view.frame.size.width, 0);
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    self.pageControl.currentPage = self.scrollerView.contentOffset.x/self.view.frame.size.width;
    
    if (self.pageControl.currentPage == self.pictureArr.count-1) {
        
        loginBut = [UIButton buttonWithType:UIButtonTypeCustom];
        loginBut.frame = CGRectMake(self.view.frame.size.width/2-100, self.view.frame.size.height-105, 200, 40);
        [loginBut setTitle:@"进入八维教育" forState:UIControlStateNormal];
        [loginBut setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [loginBut addTarget:self action:@selector(changeView) forControlEvents:UIControlEventTouchUpInside];
        
        [self.view addSubview:loginBut];
        
    }
    else
    {
        [loginBut removeFromSuperview];
    }

}

-(void)changeView
{
    
    ViewController *viewCon = [[ViewController alloc]init];
    
    [self presentViewController:viewCon animated:YES completion:nil];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
